<template>
  <div class="app-container">
    <div class="head">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="店铺名称：" prop="">
          <el-input v-model="queryParams.name" placeholder="请输入店铺名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="店铺类型：" prop="">
          <el-select v-model="queryParams.type" placeholder="请选择" clearable style="width: 240px">
            <el-option v-for="dict in shopTypeList" :key="dict.id" :label="dict.value" :value="dict.id" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="listBox">
      <div class="listBox_head">
        <el-button type="primary" style="margin: 10px 0 20px" @click="addClick">新增店铺</el-button>
      </div>
      <el-table v-loading="loading" :data="tabList" stripe border>
        <el-table-column label="序号" align="center" type="index" width="64px" />
        <el-table-column label="店铺行业" align="center" prop="industryName" :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column label="店铺类型" align="center" prop="type" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <dict-tag :options="shopTypeList" :value="scope.row.type" />
          </template>
        </el-table-column>
        <el-table-column label="店铺名称" align="center" prop="name" :show-overflow-tooltip="true" />
        <el-table-column label="店铺简称" align="center" prop="shortName" :show-overflow-tooltip="true" />
        <el-table-column label="店铺logo" align="center" prop="type" width="140">
          <template slot-scope="scope">
            <el-image style="width: 100px;" :src="scope.row.logo" fit="fill" v-if="scope.row.logo"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="定位地址" align="center" prop="address" :show-overflow-tooltip="true" />
        <el-table-column label="营业时间" align="center" prop="businessTime" :show-overflow-tooltip="true">
          <!-- <template slot-scope="scope">
            <span>{{scope.row.startTime}}</span> ~ <span>{{scope.row.endTime}}</span>
          </template> -->
        </el-table-column>
        <el-table-column label="登录账号" align="center" prop="account" :show-overflow-tooltip="true" />
        <el-table-column label="关联券数量" align="center" prop="couponCount" :show-overflow-tooltip="true" />
        <el-table-column label="账号状态" align="center" prop="" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <dict-tag :options="shopStatusList" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="排序" align="center" prop="sort" :show-overflow-tooltip="true" />
        <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="editClick(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="deactivate(scope.row,1)" v-if="scope.row.status==2">启用
            </el-button>
            <el-button size="mini" type="text" @click="deactivate(scope.row,2)" v-if="scope.row.status==1">停用
            </el-button>
            <el-button size="mini" type="text" @click="jumpWriteOffRecord(scope.row)">核销记录</el-button>
            <el-button size="mini" type="text" @click="downloadImg(scope.row)" v-if="scope.row.type==1">商家二维码</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList"></pagination>
    </div>
    <Dialog :id="dialogId" v-bind="$attrs" v-if="dialogVisible" :visible.sync="dialogVisible" @success="_handleSuccess">
    </Dialog>
  </div>
</template>

<script>
  import {
    shenyeShopList,
    editShopStatus,
    shenyeShopQrcode
  } from "@/api/consumeMiningManagement/clockShopConfiguration";
  import Dialog from "@/views/consumeMiningManagement/clockShopConfiguration/components/dialog";
  export default {
    name: "",
    components: {
      Dialog
    },
    data() {
      return {
        dialogId: '',
        dialogVisible: false,
        // 遮罩层
        loading: false,
        // 导出遮罩层
        // 总条数
        total: 0,
        shopTypeList: [], //店铺类型字典数组
        shopStatusList: [], //状态类型
        //表格数据
        tabList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: '', //店铺名称
          type: '', //店铺类型 shenye_shop_type
        }
      };
    },
    created() {
      this.getDict();
      this.getShopStatusList()
      this.getList();
    },
    methods: {
      //获取店铺类型字典
      getDict() {
        var dictType = "shenye_shop_type";
        this.$getSelectList(dictType)
          .then((res) => {
            var arr = [{
              id: "",
              value: "全部",
            }, ];
            this.shopTypeList = arr.concat(res.result[0].dictList);
          })
          .finally(() => {
            this.loading = false;
          });
      },
      //获取状态类型字典
      getShopStatusList() {
        var dictType = "shenye_shop_status";
        this.$getSelectList(dictType)
          .then((res) => {
            this.shopStatusList = res.result[0].dictList;
          })
          .finally(() => {
            this.loading = false;
          });
      },
      /** 查询列表 */
      getList() {
        this.loading = true;
        shenyeShopList(this.queryParams).then(response => {
          this.tabList = response.result.records;
          this.total = response.result.total;
          console.log(this.total)
          this.loading = false;
        });
      },
      // 表单重置
      reset() {
        this.queryParams = {
          pageNum: 1,
          pageSize: 10,
          name: '', //店铺名称
          type: '', //店铺类型 shenye_shop_type
        };
        // this.resetForm("form");
        this.handleQuery()
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        // this.resetForm("queryForm");
        this.reset();
        this.handleQuery();
      },
      //新增
      addClick() {
        this.dialogId = null
        this.dialogVisible = true;
      },
      // 编辑
      editClick(row) {
        this.dialogId = row.id
        this.dialogVisible = true;
      },
      //停用
      deactivate(row, status) {
        let title = '启用'
        if (status == 1) {
          title = '启用'
        } else {
          title = '停用'
        }
        this.$confirm('是否' + title + '?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let data = {
            id: row.id,
            status: status
          }
          editShopStatus(data).then(response => {
            this.$message({
              type: 'success',
              message: title + '成功!'
            });
            this.getList();
          });
        }).catch(() => {});
      },
      _handleSuccess(e) {
        //成功回调
        this.resetQuery()
        // this.getList()
      },
      jumpWriteOffRecord(item) { //核销记录
        this.$router.push({
          path: "/consumeMiningManagement/punchVerificationRecord",
          query: {
            id: item.id
          },
        });
      },
      downloadImg(row) { //商家二维码下载
        shenyeShopQrcode(row.id).then(res => {
          var base64ToBlob = function (code) {
            let parts = code.split(';base64,');
            let contentType = parts[0].split(':')[1];
            let raw = window.atob(parts[1]);
            let rawLength = raw.length;
            let uInt8Array = new Uint8Array(rawLength);
            for (let i = 0; i < rawLength; ++i) {
              uInt8Array[i] = raw.charCodeAt(i);
            }
            return new Blob([uInt8Array], {
              type: contentType
            });
          };
          let aLink = document.createElement('a');
          let blob = base64ToBlob(res.result); //new Blob([content]);
          let evt = document.createEvent("HTMLEvents");
          evt.initEvent("click", true, true); //initEvent 不加后两个参数在FF下会报错  事件类型，是否冒泡，是否阻止浏览器的默认行为
          aLink.download = '二维码';
          aLink.href = URL.createObjectURL(blob);
          aLink.click();
        });
      },

    }
  };

</script>
<style scoped>
  .head {
    box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    padding: 20px 20px 0;
  }

  .listBox {
    box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    padding: 20px 20px;
    margin-top: 10px;
  }

  .listBox_head {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .img_box {
    width: 100px;
    height: 80px;
    border-radius: 4px;
  }

</style>
